/*
 * 获取本机内网或外网的ip地址
 * ios系统
 * @author li.xiaomei
 * @date 2016-12-29
 */

var os = require('os');

function getLocalIPWin() {
    var map = [];
    var ifaces = os.networkInterfaces();

    for (var dev in ifaces) {
        if (dev.indexOf('eth0') != -1) {
            var tokens = dev.split(':');
            var dev2 = null;
            if (tokens.length == 2) {
                dev2 = 'eth1:' + tokens[1];
            } else if (tokens.length == 1) {
                dev2 = 'eth1';
            }
            if (null == ifaces[dev2]) {
                continue;
            }
            // 找到eth0和eth1分别的ip
            var ip = null, ip2 = null;
            ifaces[dev].forEach(function(details) {
                if (details.family == 'IPv4') {
                    ip = details.address;
                }
            });
            ifaces[dev2].forEach(function(details) {
                if (details.family == 'IPv4') {
                    ip2 = details.address;
                }
            });
            if (null == ip || null == ip2) {
                continue;
            }
            // 将记录添加到map中去
            if (ip.indexOf('10.') == 0 ||
                ip.indexOf('172.') == 0 ||
                ip.indexOf('192.') == 0) {
                map.push({"intranet_ip" : ip, "internet_ip" : ip2});
            } else {
                map.push({"intranet_ip" : ip2, "internet_ip" : ip});
            }
        }
    }
    return map;
}

function getLocalIPIos() {
    var map = [];
    var ifaces = os.networkInterfaces();
    var local = 'lo0',
        extra = 'en0',
        local_ip = '',
        extra_ip = '';

    if(ifaces[local]) {
      ifaces[local].forEach(function(item){
        if (item.family == 'IPv4') {
            local_ip = item.address;
        }
      });
    }

    if(ifaces[extra]) {
      ifaces[extra].forEach(function(item){
        if (item.family == 'IPv4') {
            extra_ip = item.address;
        }
      });
    }

    return {
      local: local_ip,
      extra: extra_ip
    };
}

module.exports = function(){
  var type = os.type();
  if(type.indexOf('Darwin') != -1) {
    return getLocalIPIos();
  }else {
    return getLocalIPWin();
  }
}
